﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Admin;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.BusinessLogic.Common;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageAdmin
{
    public partial class AdminAdd : UserControl
    {
        private static AdminLogic adminBll;
        private V_Admin adminView;
        private T_Admin admin;
        public AdminList adminList;
        public TreeLogic LgcTree;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public AdminAdd()
        {
            adminBll = new AdminLogic();
            adminView = new V_Admin();
            admin = new T_Admin();
            LgcTree = new TreeLogic();
            InitializeComponent();
            ShopLogic.BindShop(cmbShop);

            txtName.Focus();
        }

        public void SetTreeValue(string treeId, string treeName)
        {
            admin.TreeId = treeId;
            txtRole.Text = treeName;
        }

        public void GetAdmin(string id)
        {
            admin.Id = id;//点修改进来的时候给Id赋值,用于判断是添加还是修改

            string whereStr = string.Format(" where T_Admin.Id='{0}'", id);
            adminView = adminBll.GetAdminView(whereStr).FirstOrDefault();
            txtAddress.Text = adminView.Address;
            txtMobile.Text = adminView.Mobile;
            txtName.Text = adminView.Name;
            txtRemark.Text = adminView.Remark;
            txtPwd.Text = adminView.Pass;
            txtPwd2.Text = adminView.Pass;
            txtRole.Text = adminView.RoleName;
            cbIsSysUser.Checked = adminView.IsSysUser == "是" ? true : false;
            nmrRoyaltyRate.Value = adminView.RoyaltyRate;
            var adminPowerMapps = adminBll.GetAdminPowerMapps(" AdminId = '" + admin.Id + "'").ToList();
            //tvPower.Nodes.RemoveAt(0);
            LgcTree.SetAdminPowerMappTreeNode(LgcTree.GetTreeByType("AdminPower"), tvPower, adminPowerMapps);

        }

        private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked, List<T_AdminPowerMapp> adminPowerMapps)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                var adminPowerMapp = new T_AdminPowerMapp();
                if (node.Checked == nodeChecked)
                {
                    adminPowerMapp.TreeId = node.Name;
                    adminPowerMapp.AdminId = admin.Id;
                    adminPowerMapps.Add(adminPowerMapp);
                }
                if (node.Nodes.Count > 0)
                {
                    this.CheckAllChildNodes(node, nodeChecked, adminPowerMapps);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var adminPowerMapps=new List<T_AdminPowerMapp>();
            CheckAllChildNodes(tvPower.Nodes[0], true, adminPowerMapps);

            //var data=tvPower.SelectedNode;
            if (CheckInput())
            {
                SetValue(admin);


                pbClose.Enabled = false;
                btnCancel.Enabled = false;
                adminBll.SaveAdmin(admin, adminPowerMapps);
                pbClose.Enabled = true;
                btnCancel.Enabled = true;
                //if (string.IsNullOrEmpty(admin.Id))
                //{
                //    admin.Id = Guid.NewGuid().ToString();
                //    adminBll.Add(admin);
                //    //FormSysMessage.ShowSuccessMsg("添加成功！");
                //}
                //else
                //{
                //    adminBll.Update(admin);
                //    //FormSysMessage.ShowSuccessMsg("修改成功！");
                //}

                AdminList.Instance.LoadGridView();
                this.FindForm().Close();
            }
        }

        private bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                FormSysMessage.ShowSuccessMsg("请输入人员名称！");
                return false;
            }
            //if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
            //{
            //    FormSysMessage.ShowSuccessMsg("请输入密码！");
            //    return false;
            //}
            string pwd = txtPwd.Text.Trim();
            string pwd2 = txtPwd2.Text.Trim();
            if (pwd != pwd2)
            {
                FormSysMessage.ShowSuccessMsg("输入的密码不一致！");
                return false;
            }
            if (string.IsNullOrEmpty(txtRole.Text.Trim()))
            {
                FormSysMessage.ShowSuccessMsg("请选择角色！");
                return false;
            }
            return true;
        }


        public void SetValue(T_Admin admin)
        {
            admin.Name = txtName.Text.Trim();
            admin.IsSysUser = cbIsSysUser.Checked == true ? "是" : "否";
            admin.Mobile = txtMobile.Text.Trim();
            admin.Pass = txtPwd.Text;
            admin.Address = txtAddress.Text.Trim();
            admin.AddTime = DateTime.Now;
            admin.Remark = txtRemark.Text.Trim();
            admin.ShopId = cmbShop.SelectedValue.ToString();
            admin.RoyaltyRate = nmrRoyaltyRate.Value;
        }

        private void btnRole_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("AdminTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;

            admin.TreeId = formTreeSelecter.TypeId;
            txtRole.Text = formTreeSelecter.TypeText;

            tvPower.Nodes.Clear();
            var adminPowerMapps = adminBll.GetAdminPowerMapps(" AdminId = '" + admin.TreeId + "'").ToList();
            LgcTree.SetAdminPowerMappTreeNode(LgcTree.GetTreeByType("AdminPower"), tvPower, adminPowerMapps);
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            FindForm().Close();
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void tvPower_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                foreach (TreeNode node in e.Node.Nodes)
                {
                    node.Checked = e.Node.Checked;
                }
            }

        }

        private void AdminAdd_Load(object sender, EventArgs e)
        {
            if (tvPower.Nodes.Count > 0) return;
            LgcTree.SetTreeNode(LgcTree.GetTreeByType("AdminPower"), tvPower);
            tvPower.ExpandAll();
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            SPACommon.ExportToExcel(tvPower,"权限");
        }

        private void cbIsSysUser_KeyUp(object sender, KeyEventArgs e)
        {
            SPACommon.SelectNextControl(sender as Control, e);
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }
    }
}
